home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 August: Tool Chest / Dev.CD Aug 94.toast / Sample Code / Snippets / Toolbox / WaveTable Sounds / WaveTableSynthPlay.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-13  |  2.0 KB  |  111 lines  |  [TEXT/MPS ]

  1. /*
  2.     WaveTableSynthPlay     12:32:47 PM  10/13/92 • Brigham Stevens
  3.     
  4.     Shows how to use the waveTableSynth
  5.     
  6.     InitDialogs is called only to install crash handler in case
  7.     no Debugger exists.
  8.  
  9. ##
  10. ##    copy to worksheet, select, press enter
  11. ###    
  12. ##    BuildCommands:
  13. ##
  14.     C -r  WaveTableSynthPlay.c
  15.     Link -t APPL -c '????' WaveTableSynthPlay.c.o ∂
  16.         "{Libraries}"Runtime.o ∂
  17.         "{Libraries}"Interface.o ∂
  18.         -o WaveTableSynthPlay
  19.     WaveTableSynthPlay
  20.  
  21. */
  22.  
  23. #include <Dialogs.h>
  24. #include <Sound.h>
  25. #include <Memory.h>
  26.  
  27. #define WAVE_SIZE (512*50)
  28.  
  29. void WaveTableSynthPlay()
  30. {
  31.     SndChannelPtr    chan;
  32.     SndCommand        mycmd;
  33.     unsigned char    *wave;
  34.     unsigned char    *waveSurfer;
  35.     short            soundByte;
  36.     short            change = 0;
  37.     short            err;
  38.     long            tickTime;
  39.  
  40.     /* Nab some memory for a waveTable to build our sound */
  41.     wave = (char *) NewPtr (WAVE_SIZE);
  42.     if(!wave) {
  43.         DebugStr("\pNewPtr failed to make wave...");
  44.         return;
  45.     }
  46.  
  47.     /* create a channel linked with the waveTableSynth */        
  48.     chan = nil;
  49.     err = SndNewChannel (&chan, waveTableSynth, initChan0, nil);
  50.     if (err) {
  51.         DebugStr("\p error SndNewChannel [1]");
  52.         goto bail;
  53.     }
  54.     
  55.     /* generate a cool wave */
  56.     waveSurfer = wave;
  57.     for (soundByte = 0; soundByte <= WAVE_SIZE; ++soundByte) {
  58.         *waveSurfer++ = change++;
  59.         if(change > WAVE_SIZE / 4) {
  60.             change += 44;
  61.         }
  62.     }
  63.     
  64.     /* install the wave */
  65.     mycmd.cmd = waveTableCmd;
  66.     mycmd.param1 = WAVE_SIZE;
  67.     mycmd.param2 = wave;
  68.     
  69.     err = SndDoImmediate (chan, &mycmd);
  70.     if (err) {
  71.         DebugStr("\p error SndDoImmediate [1]");
  72.         goto bail;
  73.     }
  74.  
  75.     /* play the wave */
  76.     mycmd.cmd = freqCmd;
  77.     mycmd.param1 = 0;
  78.     mycmd.param2 = 20;
  79.     
  80.     err = SndDoImmediate (chan, &mycmd);
  81.     if (err) {
  82.         DebugStr("\p error SndDoImmediate [2]");
  83.         goto bail;
  84.     }
  85.  
  86.     Delay(180,&tickTime);
  87.     
  88.     /* Now dump the channel */
  89.     err = SndDisposeChannel (chan,false);
  90.     if (err) {
  91.         DebugStr("\p error SndDisposeChannel [1]");
  92.         goto bail;
  93.     }
  94.  
  95. bail:
  96.     DisposePtr(wave);
  97. }
  98.  
  99.  
  100. pascal Panic()
  101. {
  102.     /* if no debugger installed, this */
  103.     /* may save you from re-booting */
  104.     ExitToShell();
  105. }
  106.  
  107. main()
  108. {
  109.     InitDialogs((ResumeProcPtr)Panic);
  110.     WaveTableSynthPlay();
  111. }